<template>
  <div class="course-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form
          :inline="true"
          :model="formInline"
          class="demo-form-inline"
          ref="form"
        >
          <el-form-item label="课程名称" prop="courseName">
            <el-input
              v-model="formInline.courseName"
              placeholder="课程名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="formInline.status" placeholder="状态" clearable>
              <el-option label="全部" value=""></el-option>
              <el-option label="下架" value="0"></el-option>
              <el-option label="上架" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" :disabled="isLoading"
              >查询</el-button
            >
          </el-form-item>
        </el-form>

        <el-button type="primary" @click="addCourse" :disabled="isLoading"
          >+ 新建课程</el-button
        >
      </div>
      <el-table :data="courses" style="width: 100%" v-loading="isLoading">
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="courseName" label="课程名称"> </el-table-column>
        <el-table-column prop="price" label="价格"> </el-table-column>
        <el-table-column prop="sortNum" label="排序"> </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              @change="onStateChange(scope.row)"
              :disabled="scope.row.isStatusLoading"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" @click="handleContent(scope.row)"
              >内容管理</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="formInline.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="formInline.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        :disabled="isLoading"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from "vue";
import { getQueryCourses, changeState } from "@/services/course";
export default Vue.extend({
  name: "CourseListIndex",
  data() {
    return {
      courses: [],
      formInline: {
        courseName: "",
        status: "",
        pageSize: 5,
        currentPage: 1,
      },
      isLoading: false,
      totalCount: 0,
    };
  },
  created() {
    this.loadCoursesInfo();
  },
  methods: {
    onSubmit() {
      this.formInline.currentPage = 1;
      this.loadCoursesInfo();
    },
    addCourse() {
      this.$router.push({
        name: "create-course",
      });
    },
    async loadCoursesInfo() {
      const { data } = await getQueryCourses(this.formInline);
      if (data.code === "000000") {
        data.data.records.forEach((item: any) => {
          item.isStatusLoading = false;
        });
        this.courses = data.data.records;
        this.totalCount = data.data.total;
      }
    },
    handleEdit(row: any) {
      this.$router.push({
        name: "edit-course",
        params: {
          courseId: row.id,
        },
      });
    },
    handleContent(row: any) {
      this.$router.push({
        name: "section-course",
        params: {
          courseId: row.id,
        },
      });
    },
    handleSizeChange(val: number) {
      this.formInline.pageSize = val;
      this.formInline.currentPage = 1;
      this.loadCoursesInfo();
    },
    handleCurrentChange(val: number) {
      this.formInline.currentPage = val;
      this.loadCoursesInfo();
    },
    async onStateChange(course: any) {
      course.isStatusLoading = true;
      const { data } = await changeState({
        courseId: course.id,
        status: course.status,
      });
      if (data.code === "000000") {
        this.$message.success("操作成功");
      }
      course.isStatusLoading = false;
    },
  },
});
</script>

<style lang="sass" scoped>
</style>